package com.bidbid.security.service.impl;

import java.util.ArrayList;
import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataAccessException;
import org.springframework.ldap.core.DirContextOperations;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.support.AbstractContextMapper;
import org.springframework.ldap.filter.EqualsFilter;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.GrantedAuthorityImpl;
import org.springframework.security.userdetails.UserDetails;
import org.springframework.security.userdetails.UserDetailsService;
import org.springframework.security.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.bidbid.security.domain.User;
import com.bidbid.security.service.IUserService;




public class AMUserDetailsServiceImpl implements UserDetailsService {
	

	
	

	private IUserService userService;




	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException, DataAccessException {
		User operator = null;
		try {
			operator = userService.getUser(username);
		} catch (Exception e) {
			e.printStackTrace();
		}

		
		
//		System.err.println(operator.getLoginName()+","+operator.getPassword()+","+operator.getOperatorID()+","+operator.getTrueName());
		if (operator == null) {
//			logger.info("username:" + username + ",user is null!");
			throw new UsernameNotFoundException("user is null");
		}
		
//		String password=result.get(0).toString();
//		operator.setPassword(password);
//		User user = buildUser(operator);
		return null;
	}

	private User buildUser(User operator) {
		GrantedAuthority ROLE_USER = new GrantedAuthorityImpl("ROLE_USER");
		GrantedAuthority[] authorities = new GrantedAuthority[]{ROLE_USER};
//		boolean enabled = operator.isValidate() == true ? true:false;
//		User user = new User(operator.getLoginName(),operator.getPassword(),enabled,true,true,true,authorities);
//		user.setOperatorID(operator.getOperatorID());
//		user.setTrueName(operator.getTrueName());
		
		return null;
	}
}
